package com.one.friend.service.exam.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.one.common.core.constants.Constants;
import com.one.common.core.domain.TableDataInfo;
import com.one.common.core.util.ThreadLocalUtil;
import com.one.friend.domain.exam.dto.ExamQueryDTO;
import com.one.friend.domain.exam.dto.ExamRankDTO;
import com.one.friend.domain.exam.vo.ExamRankVO;
import com.one.friend.domain.exam.vo.ExamVO;
import com.one.friend.domain.user.vo.UserVO;
import com.one.friend.manager.ExamCacheManager;
import com.one.friend.manager.UserCacheManager;
import com.one.friend.mapper.exam.ExamMapper;
import com.one.friend.mapper.user.UserExamMapper;
import com.one.friend.service.exam.IExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExamServiceImpl implements IExamService {

    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private UserExamMapper userExamMapper;

    @Autowired
    private ExamCacheManager examCacheManager;

    @Autowired
    private UserCacheManager userCacheManager;

    @Override
    public List<ExamVO> list(ExamQueryDTO examQueryDTO) {
        //pagehelper帮助实现分页
        PageHelper.startPage(examQueryDTO.getPageNum(), examQueryDTO.getPageSize());
        return  examMapper.selectExamList(examQueryDTO);
    }

    @Override
    public TableDataInfo redisList(ExamQueryDTO examQueryDTO) {
        //从redis中拿取竞赛信息
        //判断redis当中是否存在数据
        Long total = examCacheManager.getListSize(examQueryDTO.getType(),null);
        List<ExamVO> examVOList;
        if(total == null || total <= 0){
            //从数据库当中拿取数据
            examVOList = list(examQueryDTO);
            //redis当中不存在数据，刷新
            examCacheManager.refreshCache(examQueryDTO.getType(),null);
            //获取一下数据总条数
            total = new PageInfo<>(examVOList).getTotal();
        }else{
            //redis当中存在数据直接拿取
            examVOList = examCacheManager.getExamVOList(examQueryDTO,null);
            //从redis当中拿取数据时也可能触发刷新，所以重新获取一下
            total = examCacheManager.getListSize(examQueryDTO.getType(),null);
        }
        if(CollectionUtil.isEmpty(examVOList)){
            return TableDataInfo.empty();
        }
        assembleExamVOList(examVOList);
        //获取符合查询条件的数据总数
        return TableDataInfo.success(examVOList,total);
    }

    @Override
    public String getFirstQuestion(Long examId) {
        //获取到当前竞赛的第一题
        //判断redis
        Long examQuestionListSize = examCacheManager.getExamQuestionListSize(examId);
        if(examQuestionListSize == null || examQuestionListSize <= 0){
            //刷新缓存
            examCacheManager.refreshExamQuestionCache(examId);
        }
        return examCacheManager.getFirstQuestion(examId).toString();
    }

    @Override
    public String examPreQuestion(Long examId,Long questionId) {
        //竞赛当中切换上下题
        Long examQuestionListSize = examCacheManager.getExamQuestionListSize(examId);
        if(examQuestionListSize == null || examQuestionListSize <= 0){
            //刷新缓存
            examCacheManager.refreshExamQuestionCache(examId);
        }
        return examCacheManager.preQuestion(examId,questionId).toString();
    }

    @Override
    public String examNextQuestion(Long examId,Long questionId) {
        Long examQuestionListSize = examCacheManager.getExamQuestionListSize(examId);
        if(examQuestionListSize == null || examQuestionListSize <= 0){
            //刷新缓存
            examCacheManager.refreshExamQuestionCache(examId);
        }
        return examCacheManager.nextQuestion(examId,questionId).toString();
    }

    @Override
    public TableDataInfo rankList(ExamRankDTO examRankDTO) {
        Long total = examCacheManager.getRankListSize(examRankDTO.getExamId());
        List<ExamRankVO> examRankVOList;
        if (total == null || total <= 0) {
            PageHelper.startPage(examRankDTO.getPageNum(), examRankDTO.getPageSize());
            examRankVOList = userExamMapper.selectExamRankList(examRankDTO.getExamId());
            examCacheManager.refreshExamRankCache(examRankDTO.getExamId());
            total = new PageInfo<>(examRankVOList).getTotal();
        } else {
            examRankVOList = examCacheManager.getExamRankList(examRankDTO);
        }
        if (CollectionUtil.isEmpty(examRankVOList)) {
            return TableDataInfo.empty();
        }
        assembleExamRankVOList(examRankVOList);
        return TableDataInfo.success(examRankVOList, total);
    }

    private void assembleExamRankVOList(List<ExamRankVO> examRankVOList) {
        if (CollectionUtil.isEmpty(examRankVOList)) {
            return;
        }
        for (ExamRankVO examRankVO : examRankVOList) {
            Long userId = examRankVO.getUserId();
            UserVO user = userCacheManager.getUserById(userId);
            examRankVO.setNickName(user.getNickName());
        }
    }

    private void assembleExamVOList(List<ExamVO> examVOList) {
        Long userId = ThreadLocalUtil.get(Constants.USER_ID, Long.class);
        List<Long> userExamIdList = examCacheManager.getAllUserExamList(userId);
        if (CollectionUtil.isEmpty(userExamIdList)) {
            return;
        }
        for (ExamVO examVO : examVOList) {
            if (userExamIdList.contains(examVO.getExamId())) {
                examVO.setEnter(true);
            }
        }
    }
}
